var getBonus = require('scenes/utility.js').getBonus;

// ==========================
// Record
// ==========================
/**
 * @param [score]
 * @param [cells]
 * @constructor
 */
function Record() {
    this.mName = 'Mr.3';
    this.mCells = [];
    this.mScore = 0;
}
Record.prototype.init = function (name, cells) {
    this.mName = name || 'Mr.3';
    this.mCells = cells || [];
    var score = 0;
    for (var i = 0; i < cells.length; i++) {
        score += getBonus(cells[i]);
    }
    this.mScore = score;
    return this;
};
Record.prototype.load = function (obj) {
    this.mName = obj.name || 'Mr.3';
    this.mScore = obj.score || 0;
    this.mCells = obj.cells || [];
    return this;
};
Record.prototype.save = function (obj) {
    obj.score = this.mScore;
    obj.cells = this.mCells;
    obj.name = this.mName;
    return obj;
};
Record.prototype.toString = function () {
    return JSON.stringify(this, null, 4);
};
Record.prototype.getScore = function () {
    return this.mScore;
};
Record.prototype.getName = function () {
    return this.mName;
};
Record.prototype.getCells = function () {
    return this.mCells;
};
Record.prototype.isFresh = function () {
    return false;
};

// ==========================
// Record
// ==========================
var _inherit = require('core/inherit.js');
/**
 * @param [score]
 * @param [cells]
 * @constructor
 */
function TempRecord(record) {
    Record.call(this);
    this.load(record.save({}));
}
_inherit(TempRecord, Record);
TempRecord.prototype.isFresh = function () {
    return true;
};

function compair(r1, r2) {
    return r2.mScore - r1.mScore;
}

Record.sort = function (records) {
    records.sort(compair);
};
Record.latestRecord = function (record) {
    return new TempRecord(record);
};

module.exports = Record;